﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Friedlich.Common.Interfaces;
using Friedlich.Common;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace Friedlich.Delicious
{



    public class DeliciousPost : ILink, ITagable, IAsyncResult 
    {

        public DeliciousPost(string title, string urn) {
            this.Title = title;
            this.Url = urn;
        }

        public string ToHref()
        {
            return string.Format("<a href=\"{0}\">{1}</a>", this.Url, this.Title);
        }

        public string Title { get; set; }

        public string Url
        {
            get;
            set;
        }


        public TagCollection Tags
        {
            get;
            set;
        }


        TagCollection _SuggestedTags;
        public TagCollection SuggestedTags {
            get {
                if (_SuggestedTags == null) {

                    DeliciousFactory factory = new DeliciousFactory();
                    _SuggestedTags = factory.GetSuggestedTags(this.Url);
                }
                return _SuggestedTags;
            }
        }



        #region IAsyncResult Members

        private AsyncCallback m_AsyncCallback;
        
        private ManualResetEvent m_ManualResetEvent;

        /// <summary>
        /// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
        /// </summary>
        /// <value></value>
        /// <returns>A user-defined object that qualifies or contains information about an asynchronous operation.</returns>
        public object AsyncState
        {
            get
            {
                return this;
            }
        }

        /// <summary>
        /// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.</returns>
        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get
            {
                return m_ManualResetEvent;
            }
        }

        /// <summary>
        /// Gets an indication of whether the asynchronous operation completed synchronously.
        /// </summary>
        /// <value></value>
        /// <returns>true if the asynchronous operation completed synchronously; otherwise, false.</returns>
        public bool CompletedSynchronously
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets an indication whether the asynchronous operation has completed.
        /// </summary>
        /// <value></value>
        /// <returns>true if the operation is complete; otherwise, false.</returns>
        public bool IsCompleted
        {
            get
            {
                return m_ManualResetEvent.WaitOne(0, false);
            }
        }
        #endregion IAsyncResult Members
    }
    
}


